#!/bin/sh
#
# K2HASH
#
# Copyright 2013 Yahoo Japan Corporation.
#
# K2HASH is key-valuew store base libraries.
# K2HASH is made for the purpose of the construction of
# original KVS system and the offer of the library.
# The characteristic is this KVS library which Key can
# layer. And can support multi-processing and multi-thread,
# and is provided safely as available KVS.
#
# For the full copyright and license information, please view
# the license file that was distributed with this source code.
#
# AUTHOR:   Tetsuya Mochizuki
# CREATE:   Thu Jan 15 2015
# REVISION:
#

#--------------------------------------------------------------
# Common Variables
#--------------------------------------------------------------
PRGNAME=$(basename "${0}")

#--------------------------------------------------------------
# Input variables
#--------------------------------------------------------------
if [ -z "$1" ]; then
	echo "[ERROR] k2hedit needs k2hash file parameter"
	exit 1
elif [ "$1" = "-h" ] || [ "$1" = "-H" ] || [ "$1" = "--help" ] || [ "$1" = "--HELP" ]; then
	echo "Usage : ${PRGNAME} --help(-h)"
	echo "        ${PRGNAME} <k2hash file>"
	echo ""
	echo " * Default editor is vi. If EDITOR environment is set, k2hedit uses it."
	echo ""
	exit 0
fi

TARGET_K2HFILE="$1"

#--------------------------------------------------------------
# Variables
#--------------------------------------------------------------
K2HTOUCH_BIN="k2htouch"

ORG_TEMPFILE="/tmp/.${PRGNAME}_$$.tmp"
WORK_TEMPFILE="/tmp/.${PRGNAME}_$$.swp"
DIFF_RESULT_FILE="/tmp/.${PRGNAME}_$$.diff"

if [ -n "${EDITOR}" ]; then
	EDITOR_BIN="${EDITOR}"
else
	EDITOR_BIN="vi"
fi

#--------------------------------------------------------------
# Main
#--------------------------------------------------------------
#
# Get list and create list file
#
if ! "${K2HTOUCH_BIN}" "${TARGET_K2HFILE}" list | sort > "${ORG_TEMPFILE}"; then
	echo "[ERROR] Failed to get list."
	exit 1
fi

#
# Make temporary file for editing
#
if ! cp "${ORG_TEMPFILE}" "${WORK_TEMPFILE}"; then
	echo "[ERROR] Could not open temporary file."
	rm -f "${ORG_TEMPFILE}"
	exit 1
fi

#
# Call editor
#
if ! "${EDITOR_BIN}" "${WORK_TEMPFILE}"; then
	echo "[ERROR] Something error occurred."
	rm -f "${ORG_TEMPFILE}"
	rm -f "${WORK_TEMPFILE}"
	exit 1
fi

#
# Check if edited
#
if ! diff --new-line-format="%L" --unchanged-line-format="" --old-line-format="" "${ORG_TEMPFILE}" "${WORK_TEMPFILE}" > "${DIFF_RESULT_FILE}"; then
	echo "[ERROR] Could not compare original file and modified file."
	rm -f "${ORG_TEMPFILE}"
	rm -f "${WORK_TEMPFILE}"
	exit 1
fi

AWK_PARAMETER="{system(\"${K2HTOUCH_BIN} ${TARGET_K2HFILE} set \"\$1\" \"\$2\"\")}"
if ! awk -F\\t "${AWK_PARAMETER}" "${DIFF_RESULT_FILE}"; then
	echo "[ERROR] Failed to set modified data to ${TARGET_K2HFILE}"
	rm -f "${ORG_TEMPFILE}"
	rm -f "${WORK_TEMPFILE}"
	rm -f "${DIFF_RESULT_FILE}"
	exit 1
fi

rm -f "${ORG_TEMPFILE}"
rm -f "${WORK_TEMPFILE}"
rm -f "${DIFF_RESULT_FILE}"

exit 0

#
# Local variables:
# tab-width: 4
# c-basic-offset: 4
# End:
# vim600: noexpandtab sw=4 ts=4 fdm=marker
# vim<600: noexpandtab sw=4 ts=4
#
